package com.applet.mallappletbackstage.service.impl;

import com.applet.mallappletbackstage.entity.ReleaseWork;
import com.applet.mallappletbackstage.entity.UserReleaseWork;
import com.applet.mallappletbackstage.mapper.ReleaseWorkMapper;
import com.applet.mallappletbackstage.mapper.UserReleaseWorkMapper;
import com.applet.mallappletbackstage.service.UserReleaseWorkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-07-31
 */
@Service
public class UserReleaseWorkServiceImpl extends ServiceImpl<UserReleaseWorkMapper, UserReleaseWork> implements UserReleaseWorkService {

    @Autowired
    ReleaseWorkMapper releaseWorkMapper;

    /***
     * 通过用户发布工作关联表查询工作
     * @param id
     * @return
     */
    @Override
    public List<String> selectReleaseWorkListByUserId(String id) {
        //条件构造
        QueryWrapper<UserReleaseWork> releaseWorkQueryWrapper = new QueryWrapper<>();
        releaseWorkQueryWrapper.eq("user_id", id);
        ArrayList<String> releaseWorkIds = new ArrayList<>();
        //用户发布工作数据集合
        List<UserReleaseWork> userReleaseWorks = baseMapper.selectList(releaseWorkQueryWrapper);
        //遍历
        for (UserReleaseWork userReleaseWork : userReleaseWorks) {
            String releaseWorkId = userReleaseWork.getReleaseWorkId();
            releaseWorkIds.add(releaseWorkId);
        }
        return releaseWorkIds;
    }

    @Override
    public List<ReleaseWork> selectWorkById(String id, int status) {
        //条件构造
        QueryWrapper<UserReleaseWork> releaseWorkQueryWrapper = new QueryWrapper<>();
        releaseWorkQueryWrapper.eq("user_id", id);
        ArrayList<String> releaseWorkIds = new ArrayList<>();
        //用户发布工作数据集合
        List<UserReleaseWork> userReleaseWorks = baseMapper.selectList(releaseWorkQueryWrapper);
        //遍历
        for (UserReleaseWork userReleaseWork : userReleaseWorks) {
            String releaseWorkId = userReleaseWork.getReleaseWorkId();
            releaseWorkIds.add(releaseWorkId);
        }
        ArrayList<ReleaseWork> releaseWorks = new ArrayList<>();
        //遍历
        for (String releaseWorkId : releaseWorkIds) {
            QueryWrapper<ReleaseWork> releaseWorkQueryWrapper1 = new QueryWrapper<>();
            releaseWorkQueryWrapper1.eq("id", releaseWorkId);
            if (status != 0) {
                releaseWorkQueryWrapper1.eq("status", status);
            }
            ReleaseWork releaseWork = releaseWorkMapper.selectOne(releaseWorkQueryWrapper1);
            releaseWorks.add(releaseWork);
        }
        return releaseWorks;
    }
}
